﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFPlpUsageManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFPlpUsageManager
{
  private const string XML_USAGE_FILE_NAME = "ListOfValues.xml";
  private const string XML_PLP_USAGE_TOOL_TYPE_ELEMENT = "toolType";
  private const string XML_PLP_USAGE_CLASS_NAME = "PmPLPUsage";
  private const string XML_META_DATA = "MetaDataExtensions";
  private const string XML_ENUM_FIELD = "EnumerationField";
  private const string XML_DISPLAYED_NAME_ATTRIBUTE = "DisplayedName";
  private const string XML_DATA_ATTRIBUTE = "data";
  private Dictionary<long, string> m_toolTypes = new Dictionary<long, string>();
  private Dictionary<ITxObject, ITxObject> m_plpToUsageMap = new Dictionary<ITxObject, ITxObject>();

  public void ClearData()
  {
    if (this.m_toolTypes != null)
      this.m_toolTypes.Clear();
    if (this.m_plpToUsageMap == null)
      return;
    this.m_plpToUsageMap.Clear();
  }

  public CApMFMappingReportUtils.EDataStatus InitToolTypes()
  {
    CApMFMappingReportUtils.EDataStatus edataStatus = CApMFMappingReportUtils.EDataStatus.dataValid;
    string filename = Path.Combine(TxApplicationEx.SysRootAppDataPath(), "ListOfValues.xml");
    XmlDocument loadedDoc = new XmlDocument();
    bool flag = true;
    try
    {
      loadedDoc.Load(filename);
    }
    catch (Exception ex)
    {
      flag = false;
      edataStatus = CApMFMappingReportUtils.EDataStatus.dataNotLoaded;
    }
    if (flag)
    {
      string className = "PmPLPUsage";
      string fieldName = "toolType";
      if (!this.FillValuesFromXML(ref this.m_toolTypes, ref loadedDoc, className, fieldName))
        edataStatus = CApMFMappingReportUtils.EDataStatus.dataInvalid;
    }
    return edataStatus;
  }

  private bool FillValuesFromXML(
    ref Dictionary<long, string> map,
    ref XmlDocument loadedDoc,
    string className,
    string fieldName)
  {
    bool flag1 = true;
    try
    {
      map.Clear();
      XmlNodeList elementsByTagName = loadedDoc.GetElementsByTagName("MetaDataExtensions");
      if (elementsByTagName != null)
      {
        if (elementsByTagName.Count > 0)
        {
          bool flag2 = false;
          for (int index = 0; index < elementsByTagName.Count && !flag2; ++index)
          {
            XmlNodeList childNodes1 = elementsByTagName.Item(index).ChildNodes;
            if (childNodes1 != null && childNodes1.Count > 0)
            {
              foreach (XmlNode xmlNode1 in childNodes1)
              {
                if (xmlNode1.Name == "EnumerationField" && xmlNode1.Attributes.Count > 1 && xmlNode1.Attributes.Item(0).Value == fieldName && xmlNode1.Attributes.Item(1).Value == className)
                {
                  XmlNodeList childNodes2 = xmlNode1.ChildNodes;
                  if (childNodes2 != null && childNodes2.Count > 0)
                  {
                    foreach (XmlNode xmlNode2 in childNodes2)
                    {
                      if (xmlNode2.Attributes.Count > 1 && xmlNode2.Attributes.Item(0).Name.Equals("data") && xmlNode2.Attributes.Item(1).Name.Equals("DisplayedName"))
                      {
                        bool flag3 = true;
                        string s = xmlNode2.Attributes.Item(0).Value;
                        long key;
                        try
                        {
                          key = long.Parse(s);
                        }
                        catch (Exception ex)
                        {
                          key = 0L;
                          flag3 = false;
                        }
                        if (flag3)
                        {
                          string str = xmlNode2.Attributes.Item(1).Value;
                          if (!map.ContainsKey(key))
                            map.Add(key, str);
                        }
                      }
                    }
                  }
                  flag2 = true;
                  break;
                }
              }
            }
          }
          flag1 = flag2;
        }
      }
    }
    catch (Exception ex)
    {
      map.Clear();
      flag1 = false;
    }
    return flag1;
  }

  public bool CachePlpUsageData()
  {
    bool flag = true;
    try
    {
      ITxPlanningObject currentStudy = TxApplication.ActiveDocument.CurrentStudy;
      if (currentStudy != null && currentStudy.IsObjectLoaded())
      {
        TxObjectList rootList1 = new TxObjectList();
        TxObjectList rootList2 = new TxObjectList();
        TxObjectList allDescendants = (currentStudy.EngineeringRepresentation as ITxObjectCollection).GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxMfgFeature)));
        if (allDescendants != null && ((Collection<ITxObject>) allDescendants).Count > 0)
        {
          foreach (TxMfgFeature txMfgFeature in (Collection<ITxObject>) allDescendants)
          {
            ITxPlanningObject planningRepresentation = CApMFMappingReportUtils.GetPlanningRepresentation((ITxObject) txMfgFeature);
            if (planningRepresentation != null && planningRepresentation.IsDerivedFromPlanningType("PmPLP"))
            {
              TxObjectList assignedOperations = txMfgFeature.AssignedOperations;
              if (assignedOperations != null)
              {
                if (((Collection<ITxObject>) assignedOperations).Count > 0)
                {
                  try
                  {
                    foreach (ITxOperation itxOperation in (Collection<ITxObject>) assignedOperations)
                    {
                      if (itxOperation != null && !((Collection<ITxObject>) rootList2).Contains((ITxObject) itxOperation))
                        ((Collection<ITxObject>) rootList2).Add((ITxObject) itxOperation);
                    }
                  }
                  catch (Exception ex)
                  {
                  }
                }
              }
            }
          }
        }
        if (((Collection<ITxObject>) rootList2).Count > 0)
        {
          CApMFMappingReportUtils.CacheData(rootList2, "operationUsages", "mfgUsages");
          foreach (ITxObject itxObject1 in (Collection<ITxObject>) rootList2)
          {
            ITxPlanningObject planningRepresentation1 = CApMFMappingReportUtils.GetPlanningRepresentation(itxObject1);
            if (planningRepresentation1 != null && planningRepresentation1.FieldWasImported("mfgUsages"))
            {
              object field = planningRepresentation1.GetField("mfgUsages");
              if (field != null && field is TxObjectList txObjectList && ((Collection<ITxObject>) txObjectList).Count > 0)
              {
                foreach (ITxObject itxObject2 in (Collection<ITxObject>) txObjectList)
                {
                  ITxPlanningObject planningRepresentation2 = CApMFMappingReportUtils.GetPlanningRepresentation(itxObject2);
                  if (planningRepresentation2 != null && planningRepresentation2.IsDerivedFromPlanningType("PmPLPUsage") && !((Collection<ITxObject>) rootList1).Contains((ITxObject) planningRepresentation2))
                    ((Collection<ITxObject>) rootList1).Add((ITxObject) planningRepresentation2);
                }
              }
            }
          }
        }
        if (((Collection<ITxObject>) rootList1).Count > 0)
        {
          CApMFMappingReportUtils.CacheData(rootList1, "plpUsages", "toolType", "uses");
          foreach (ITxObject plpUsage in (Collection<ITxObject>) rootList1)
          {
            if (plpUsage != null)
              this.MapPlpToUsage(plpUsage);
          }
        }
      }
      else
        flag = false;
    }
    catch (Exception ex)
    {
      flag = false;
    }
    return flag;
  }

  private void MapPlpToUsage(ITxObject plpUsage)
  {
    ITxPlanningObject planningRepresentation1 = CApMFMappingReportUtils.GetPlanningRepresentation(plpUsage);
    if (planningRepresentation1 == null || !planningRepresentation1.FieldWasImported("uses"))
      return;
    object field1 = planningRepresentation1.GetField("uses");
    if (field1 == null || !(field1 is ITxObject itxObject))
      return;
    ITxPlanningObject planningRepresentation2 = CApMFMappingReportUtils.GetPlanningRepresentation(itxObject);
    if (planningRepresentation2 == null || !planningRepresentation2.IsDerivedFromPlanningType("PmPLP") || this.m_plpToUsageMap.ContainsKey((ITxObject) planningRepresentation2) || !planningRepresentation1.FieldWasImported("toolType"))
      return;
    object field2 = planningRepresentation1.GetField("toolType");
    if (field2 == null || !this.m_toolTypes.ContainsKey((long) field2))
      return;
    this.m_plpToUsageMap.Add((ITxObject) planningRepresentation2, (ITxObject) planningRepresentation1);
  }

  public ITxObject GetMappedPlpUsage(ITxObject plp)
  {
    ITxObject mappedPlpUsage = (ITxObject) null;
    if (plp != null)
    {
      ITxPlanningObject planningRepresentation = CApMFMappingReportUtils.GetPlanningRepresentation(plp);
      if (this.m_plpToUsageMap.ContainsKey((ITxObject) planningRepresentation))
        mappedPlpUsage = this.m_plpToUsageMap[(ITxObject) planningRepresentation];
    }
    return mappedPlpUsage;
  }

  public object GetMappedPlpUsageField(ITxObject plp, string fieldName)
  {
    object mappedPlpUsageField = (object) null;
    ITxObject mappedPlpUsage = this.GetMappedPlpUsage(plp);
    if (mappedPlpUsage != null)
    {
      ITxPlanningObject planningRepresentation = CApMFMappingReportUtils.GetPlanningRepresentation(mappedPlpUsage);
      if (planningRepresentation != null && fieldName == "toolType")
      {
        object field = planningRepresentation.GetField("toolType");
        if (field != null)
        {
          long key = (long) field;
          if (this.m_toolTypes.ContainsKey(key))
            mappedPlpUsageField = (object) this.m_toolTypes[key];
        }
      }
    }
    return mappedPlpUsageField;
  }
}
